﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Media;

namespace Behaviour.Selection
{
    public class SelectionAdorner : Adorner
    {
        private SelectionThumb[] _thumbs;

        private readonly VisualCollection _visuals;

        public SelectionAdorner(UIElement adornedElement) : base(adornedElement)
        {
            _thumbs = new SelectionThumb[4];
            _visuals = new VisualCollection(this);
            ProcessThumb(out _thumbs[0]);
            ProcessThumb(out _thumbs[1]);
            ProcessThumb(out _thumbs[2]);
            ProcessThumb(out _thumbs[3]);
        }

        private void ProcessThumb(out SelectionThumb thumb)
        {
            thumb = new SelectionThumb();
            thumb.Width = 5;
            thumb.Height = 5;
            thumb.BorderBrush = Brushes.Black;
            thumb.BorderThickness = new Thickness(1);
            _visuals.Add(thumb);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            double adornerWidth = DesiredSize.Width;
            double adornerHeight = DesiredSize.Height;
            double adornerElWidth = AdornedElement.DesiredSize.Width;
            double adornerElHeight = AdornedElement.DesiredSize.Height;

            _thumbs[0].Arrange(new Rect(-adornerElWidth/2, -adornerElHeight/2, adornerWidth, adornerHeight));
            _thumbs[1].Arrange(new Rect(adornerElWidth / 2, -adornerElHeight / 2, adornerWidth, adornerHeight));
            _thumbs[2].Arrange(new Rect(-adornerElWidth / 2, adornerElHeight / 2, adornerWidth, adornerHeight));
            _thumbs[3].Arrange(new Rect(adornerElWidth / 2, adornerElHeight / 2, adornerWidth, adornerHeight));

            return finalSize;
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return _visuals.Count;
            }
        }

        protected override Visual GetVisualChild(int index)
        {
            return _visuals[index];
        }


    }
}
